package com.fa4j.system.account.service;

import com.fa4j.common.base.util.FillUtil;
import com.fa4j.system.account.api.AccountClient;
import com.fa4j.system.account.dto.AccountDTO;
import com.fa4j.system.account.model.entity.Operator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 操作人填充服务
 */
@Service
@RequiredArgsConstructor
public class OperatorFillService {

    private final AccountClient accountClient;

    /**
     * 查询操作人名称映射关系
     */
    public Map<Long, String> queryOperatorMap(Set<Long> operatorIds) {
        List<AccountDTO> accountDTOList = accountClient.queryAccountsByIds(operatorIds);
        if (accountDTOList == null || accountDTOList.isEmpty()) {
            return Map.of();
        }
        return accountDTOList.stream().collect(Collectors.toMap(AccountDTO::getAccountId, AccountDTO::getNickname));
    }

    /**
     * 填充操作人信息
     *
     * @param dataList 需要填充的数据
     */
    public void fill(Collection<? extends Operator> dataList) {
        FillUtil.fillField(dataList, Operator::getOperatorId, Operator::setOperatorName, this::queryOperatorMap);
    }

    /**
     * 填充操作人信息
     *
     * @param data 需要填充的数据
     */
    public <T extends Operator> T fill(T data) {
        fill(List.of(data));
        return data;
    }
}
